/*
 * AddFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.wdzl.frames;

import com.wdzl.biz.DeptBiz;
import com.wdzl.biz.EmpBiz;
import com.wdzl.entity.Depart;
import com.wdzl.entity.Employee;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author __USER__
 */
public class AddFrame extends javax.swing.JFrame {

    private com.wdzl.frames.MainFrame mf;
    private DeptBiz deptBiz = new DeptBiz();
    private EmpBiz empBiz = new EmpBiz();

    /**
     * Creates new form AddFrame
     */
    public AddFrame(com.wdzl.frames.MainFrame mf) {
        this.mf = mf;
        initComponents();
    }


    private void initComponents() {

        jLabel1 = new JLabel();
        jSeparator1 = new JSeparator();
        jLabel2 = new JLabel();
        jTextField1 = new JTextField();
        jLabel3 = new JLabel();
        ButtonGroup group = new ButtonGroup();
        jRadioButton1 = new JRadioButton();
        jRadioButton1.setSelected(true);
        jRadioButton2 = new JRadioButton();
        group.add(jRadioButton1);
        group.add(jRadioButton2);
        jLabel4 = new JLabel();
        jComboBox1 = new JComboBox();
        jLabel5 = new JLabel();
        jTextField2 = new JTextField();
        jLabel6 = new JLabel();
        jTextField3 = new JTextField();
        jLabel7 = new JLabel();
//		jLabel8 = new JLabel();
        jTextField4 = new JTextField();
//		jTextField5 = new JTextField();
        jButton1 = new JButton();
        jButton2 = new JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("楷体_GB2312", 1, 24));
        jLabel1.setText("添加新员工");

        jLabel2.setText("姓名：");

        jLabel3.setText("性别：");

        jRadioButton1.setText("男");

        jRadioButton2.setText("女");

        jLabel4.setText("部门：");
        Object[] objs = deptBiz.getDeaprts().toArray();
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(objs));

        jLabel5.setText("薪资：");
        jLabel6.setText("邮箱：");

        jLabel7.setText("手机号");

        jButton1.setText("添加");

        jButton1.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                String name = jTextField1.getText();
                if (name.equals("")) {
                    JOptionPane.showMessageDialog(null, "姓名不能为空！", "错误信息", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                String sex = "";
                if (jRadioButton1.isSelected()) {
                    sex = "男";
                }
                if (jRadioButton2.isSelected()) {
                    sex = "女";
                }
                Depart d = (Depart) jComboBox1.getSelectedItem();
                int did = d.getId();
                double money = 0;
                String salary = jTextField2.getText();
                try {
                    if (!salary.equals(""))
                        money = Double.parseDouble(salary);
                    else {
                        JOptionPane.showMessageDialog(null, "薪资不能为空！", "错误信息", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                } catch (Exception e2) {
                    // TODO: handle exception
                    JOptionPane.showMessageDialog(null, "开始工资非法！", "错误信息", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                String email = "";
                String Email = jTextField3.getText();
                try {
                    if (!Email.equals(""))
                        email = String.valueOf(Email);
                    else {
                        JOptionPane.showMessageDialog(null, "邮箱不能为空！", "错误信息", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                } catch (Exception e2) {
                    // TODO: handle exception
                    JOptionPane.showMessageDialog(null, "填写邮箱非法！", "错误信息", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                String number = "";
                String pNumber = jTextField4.getText();
                try {
                    if (!pNumber.equals(""))
                        number = String.valueOf(pNumber);
                    else {
                        JOptionPane.showMessageDialog(null, "手机号不能为空！", "错误信息", JOptionPane.ERROR_MESSAGE);
                        return;
                    }
                } catch (Exception e2) {
                    // TODO: handle exception
                    JOptionPane.showMessageDialog(null, "填写手机号非法！", "错误信息", JOptionPane.ERROR_MESSAGE);
                    return;
                }
                Employee emp = new Employee(name, sex, did, money, email, number);
                if (empBiz.addEmp(emp) > 0) {
                    JOptionPane.showMessageDialog(null, "添加成功！", "", JOptionPane.INFORMATION_MESSAGE);

                    mf.setTableValue();
                } else {
                    JOptionPane.showMessageDialog(null, "添加失败！", "", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        jButton2.setText("取消");
        jButton2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                AddFrame.this.setVisible(false);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout
                .setHorizontalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                        .createSequentialGroup()
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addGroup(
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addGap(
                                                                                130,
                                                                                130,
                                                                                130)
                                                                        .addComponent(
                                                                                jLabel1))
                                                        .addGroup(
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addGap(
                                                                                93,
                                                                                93,
                                                                                93)
                                                                        .addGroup(
                                                                                layout
                                                                                        .createParallelGroup(
                                                                                                javax.swing.GroupLayout.Alignment.LEADING,
                                                                                                false)
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel2)
                                                                                                        .addGap(
                                                                                                                18,
                                                                                                                18,
                                                                                                                18)
                                                                                                        .addComponent(
                                                                                                                jTextField1))
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel3)
                                                                                                        .addGap(
                                                                                                                18,
                                                                                                                18,
                                                                                                                18)
                                                                                                        .addComponent(
                                                                                                                jRadioButton1)
                                                                                                        .addGap(
                                                                                                                18,
                                                                                                                18,
                                                                                                                18)
                                                                                                        .addComponent(
                                                                                                                jRadioButton2))
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel4)
                                                                                                        .addGap(
                                                                                                                18,
                                                                                                                18,
                                                                                                                18)
                                                                                                        .addComponent(
                                                                                                                jComboBox1,
                                                                                                                0,
                                                                                                                GroupLayout.DEFAULT_SIZE,
                                                                                                                Short.MAX_VALUE))
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel6)
                                                                                                        .addGap(
                                                                                                                18,
                                                                                                                18,
                                                                                                                18)
                                                                                                        .addComponent(
                                                                                                                jTextField3))
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel7)
                                                                                                        .addGap(
                                                                                                                18,
                                                                                                                18,
                                                                                                                18)
                                                                                                        .addComponent(
                                                                                                                jTextField4))
//																						.addGroup(
//																								layout
//																										.createSequentialGroup()
//																										.addComponent(
//																												jLabel8)
//																										.addGap(
//																												18,
//																												18,
//																												18)
//																										.addComponent(
//																												jTextField5))
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jLabel5)
                                                                                                        .addGap(
                                                                                                                18,
                                                                                                                18,
                                                                                                                18)
                                                                                                        .addComponent(
                                                                                                                jTextField2,
                                                                                                                GroupLayout.PREFERRED_SIZE,
                                                                                                                111,
                                                                                                                GroupLayout.PREFERRED_SIZE))
                                                                                        .addGroup(
                                                                                                layout
                                                                                                        .createSequentialGroup()
                                                                                                        .addComponent(
                                                                                                                jButton1)
                                                                                                        .addPreferredGap(
                                                                                                                LayoutStyle.ComponentPlacement.RELATED,
                                                                                                                GroupLayout.DEFAULT_SIZE,
                                                                                                                Short.MAX_VALUE)
                                                                                                        .addComponent(
                                                                                                                jButton2))))
                                                        .addGroup(
                                                                layout
                                                                        .createSequentialGroup()
                                                                        .addGap(
                                                                                62,
                                                                                62,
                                                                                62)
                                                                        .addComponent(
                                                                                jSeparator1,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                                                237,
                                                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(56, 56, 56)));
        layout
                .setVerticalGroup(layout
                        .createParallelGroup(
                                javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                layout
                                        .createSequentialGroup()
                                        .addContainerGap()
                                        .addComponent(jLabel1)
                                        .addPreferredGap(
                                                LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(
                                                jSeparator1,
                                                GroupLayout.PREFERRED_SIZE,
                                                10,
                                                GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(
                                                LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel2)
                                                        .addComponent(
                                                                jTextField1,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel3)
                                                        .addComponent(
                                                                jRadioButton1)
                                                        .addComponent(
                                                                jRadioButton2))
                                        .addGap(18, 18, 18)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel6)
                                                        .addComponent(
                                                                jTextField3,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel7)
                                                        .addComponent(
                                                                jTextField4,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
//										.addGroup(
//												layout
//														.createParallelGroup(
//																GroupLayout.Alignment.BASELINE)
//														.addComponent(jLabel8)
//														.addComponent(
//																jTextField5,
//																GroupLayout.PREFERRED_SIZE,
//																GroupLayout.DEFAULT_SIZE,
//																GroupLayout.PREFERRED_SIZE))
//										.addGap(18, 18, 18)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel4)
                                                        .addComponent(
                                                                jComboBox1,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jLabel5)
                                                        .addComponent(
                                                                jTextField2,
                                                                GroupLayout.PREFERRED_SIZE,
                                                                GroupLayout.DEFAULT_SIZE,
                                                                GroupLayout.PREFERRED_SIZE))
                                        .addGap(18, 18, 18)
                                        .addGroup(
                                                layout
                                                        .createParallelGroup(
                                                                GroupLayout.Alignment.BASELINE)
                                                        .addComponent(jButton1)
                                                        .addComponent(jButton2))
                                        .addContainerGap(40, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>
    //GEN-END:initComponents

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private JButton jButton1;
    private JButton jButton2;
    private JComboBox jComboBox1;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    private JLabel jLabel4;
    private JLabel jLabel5;
    private JLabel jLabel6;
    private JLabel jLabel7;
    //	private JLabel jLabel8;
    private JRadioButton jRadioButton1;
    private JRadioButton jRadioButton2;
    private JSeparator jSeparator1;
    private JTextField jTextField1;
    private JTextField jTextField2;
    private JTextField jTextField3;
    private JTextField jTextField4;
//	private JTextField jTextField5;
    // End of variables declaration//GEN-END:variables

}